﻿// -----------------------------------------------------------------------
// <copyright file="MainViewModel.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Sigma.DrBlue.WpfUI.ViewModels
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Globalization;
    using System.IO;
    using System.Linq;
    using Microsoft.Practices.Prism.Commands;
    using Microsoft.Practices.Prism.ViewModel;
    using Sigma.DrBlue.WpfUI.LibCore;
    using Sigma.DrBlue.WpfUI.Models;

    public class MainViewModel : NotificationObject
    {
        private string _folderPath;

        public string FolderPath
        {
            get { return _folderPath; }
            set
            {
                if (this._folderPath == value)
                {
                    return;
                }

                _folderPath = value;
                RaisePropertyChanged(() => this.FolderPath);
            }
        }

        private ObservableCollection<CultureModel> collectionCultures = new ObservableCollection<CultureModel>();
        public ObservableCollection<CultureModel> Cultures
        {
            get
            {
                return collectionCultures;
            }
        }

        private ObservableCollection<StringResourceModel> collectionStringResources = new ObservableCollection<StringResourceModel>();
        public ObservableCollection<StringResourceModel> StringResources
        {
            get
            {
                return collectionStringResources;
            }
        }

        private ObservableCollection<AssemblyModel> collectionAssembies = new ObservableCollection<AssemblyModel>();
        public ObservableCollection<AssemblyModel> Assemblies
        {
            get
            {
                return this.collectionAssembies;
            }
        }

        public MainViewModel()
        {
            
        }


        private DelegateCommand parseCommand;
        public DelegateCommand ParseCommand
        {
            get
            {
                if (parseCommand == null)
                {
                    parseCommand = new DelegateCommand(this.Parse);
                }

                return parseCommand;
            }
        }

        private DelegateCommand addFolderCommand;
        public DelegateCommand AddFolderCommand
        {
            get
            {
                if (this.addFolderCommand == null)
                {
                    this.addFolderCommand = new DelegateCommand(this.AddFolder);
                }

                return this.addFolderCommand;
            }
        }

        public void Parse()
        {
            CultureInfo[] cultures =  CultureInfo.GetCultures(CultureTypes.AllCultures);

            string file = @"C:\Program Files\Microsoft System Center 2012 Virtual Machine Manager\bin\Microsoft.VirtualManager.UI.Dialogs.dll";

            List<CultureInfo> lstCultures = new List<CultureInfo>();
            string[] directories = Directory.GetDirectories(new FileInfo(file).DirectoryName);
            foreach (string dir in directories)
            {
                DirectoryInfo dirInfo = new DirectoryInfo(dir);

                CultureInfo culture = (from c in cultures where c.Name.Equals(dirInfo.Name, StringComparison.InvariantCultureIgnoreCase) select c).SingleOrDefault();
                if (culture != null)
                {
                    lstCultures.Add(culture);
                }
            }

            // string file = @"F:\MyProjects\Redmond\Smart Tools\DrBlue\Sigma.DrBlue.WpfUI\bin\Debug - Copy\Sigma.DrBlue.WpfUI.exe";

            AssemblyModel asmModel = AsmParser.ParseResourceFromAssembly(file, lstCultures);

            foreach (ResourceModel resource in asmModel.Resources)
            {
                foreach (StringResourceModel stringResource in resource.StringResources)
                {
                    collectionStringResources.Add(stringResource);
                }
            }
        }

        public void AddFolder()
        {
            
        }
    }
}
